package api

import (
	"api-getway/basic/config"
	__ "api-getway/basic/proto"
	"api-getway/handler/request"
	"github.com/gin-gonic/gin"
	"net/http"
)

func SendSms(c *gin.Context) {
	var err error
	var req request.SendSmsReq

	if err = c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": err.Error(),
		})
		return
	}

	_, err = config.VideoClient.SendSms(c, &__.SendSmsReq{
		Mobile: req.Mobile,
	})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": 500,
			"msg":  err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "短信验证码发送成功",
	})
}

func Register(c *gin.Context) {
	var err error
	var req request.RegisterReq

	if err = c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": err.Error(),
		})
		return
	}

	register, err := config.VideoClient.Register(c, &__.RegisterReq{
		Mobile:   req.Mobile,
		SmsCode:  req.SmsCode,
		Password: req.Password,
	})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": 500,
			"msg":  "账号注册失败",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":   200,
		"msg":    "注册成功",
		"userId": register.Id,
	})
}

func Login(c *gin.Context) {
	var err error
	var req request.LoginReq

	if err = c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": err.Error(),
		})
		return
	}

	login, err := config.VideoClient.Login(c, &__.LoginReq{
		Mobile:  req.Mobile,
		SmsCode: req.SmsCode,
	})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": 500,
			"msg":  "登陆失败",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":   200,
		"msg":    "登陆成功",
		"userId": login.Id,
	})
}
